package com.hp.attendance.dao;

import com.hp.attendance.entity.*;
import com.hp.attendance.util.BaseDAO;
import javafx.geometry.Pos;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class EmployeeBysDao extends BaseDAO {

//    --------------------------查询方法提供--------------------
    /**
     * 查询在职员工信息
     * @return
     */
    public List<Employee> getEmpList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from employee";
        List<Employee> list = queryList(sql, new Object[0], new Employee());


        return list;
    }




    /**
     * 根据部门编号查询部门名称
     * @param department_number
     * @return
     */
    public Department getDepName(int department_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from department where department_number=?";
        Object[] obj = {department_number};
        Department department = queryOne(sql, obj, new Department());
        return department;
    }


    /**
     * 根据职位编号查询职位名称
     * @param position_number
     * @return
     */
    public Position getPosName(int position_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from position where position_number=?";
        Object[] obj = {position_number};
        Position position = queryOne(sql, obj, new Position());
        return position;
    }

//    --------------------------分页查询--------------------

    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<Employee> getLimitList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from employee limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Employee> employees = queryList(sql, obj, new Employee());
        return employees;
    }


    /**
     * 查询总记录数
     * @return
     * @throws SQLException
     */
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from history where status='在职'";
        int count = queryCount(sql, new Object[0]);
        return count;
    }

    //    --------------------------添加员工信息--------------------

    /**
     * 添加员工信息
     * @param employee
     */
    public void AddEmployee(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        String sql = "insert into employee values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] obj = {employee.getEmployee_number(),employee.getName(),employee.getGender(),employee.getBirthday(),employee.getTelephone(),
        employee.getEmail(),employee.getAddress(),employee.getPhoto(),employee.getEducation(),employee.getDepartment_number(),
        employee.getPosition_number(),employee.getIn_time(),employee.getPassword(),employee.getNotes()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 获取部门信息
     * @return
     */
    public List<Department> getDepartment() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select*from department";
        List<Department> list = queryList(sql, new Object[0], new Department());
        return list;
    }

    /**
     * 获取职称信息
     * @return
     */
    public List<Position> getPosition() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select*from position";
        List<Position> list = queryList(sql, new Object[0], new Position());
        return list;
    }


    /**
     * 根据工号查询employee信息
     * @param employee_number
     * @return
     */
    public Employee getEmployeeByNum(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from employee where employee_number=?";
        Object[] obj = {employee_number};
        Employee employee = queryOne(sql, obj, new Employee());
        return employee;
    }


    /**
     * 修改员工信息
     * @param employee
     */
    public void UpdateEmployeeByNum(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        String sql = "update employee set name=?,gender=?,birthday=?,telephone=?,email=?,address=?,education=?," +
                "department_number=?,position_number=?,in_time=?,password=?,notes=? where employee_number=?";
        Object[] obj = {employee.getName(),employee.getGender(),employee.getBirthday(),employee.getTelephone(),
        employee.getEmail(),employee.getAddress(),employee.getEducation(),employee.getDepartment_number(),
        employee.getPosition_number(),employee.getIn_time(),employee.getPassword(),employee.getNotes(),employee.getEmployee_number()};
        addOrUpdOrDel(sql,obj);
    }


    /**
     * 修改在职状态
     * @param status
     * @param employee_number
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void UpdateHistoryByNum(String status,int employee_number) throws SQLException, IOException, ClassNotFoundException {
        String sql = "update history set status=? where employee_number=?";
        Object[] obj = {status,employee_number};
        addOrUpdOrDel(sql,obj);
    }

    /**
     *  修改离职或退休时间
     * @param date
     * @param employee_number
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */


    public void UpdateOut_timeByNum(Date date, int employee_number) throws SQLException, IOException, ClassNotFoundException {
        String sql = "update history set out_time=? where employee_number=?";
        Object[] obj = {date,employee_number};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 删除员工信息
     * @param employee_number
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void DelEmployee(int employee_number) throws SQLException, IOException, ClassNotFoundException {
        String sql = "delete from employee where employee_number=?";
        Object[] obj = {employee_number};
        addOrUpdOrDel(sql,obj);
    }

}
